package com.example.cookbook.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.cookbook.R;
import com.example.cookbook.bean.RecipeBean;
import com.example.cookbook.activity.RecipeDetailActivity;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> {
    private Context mcontext;
    private List<RecipeBean> arrlist2;
    public RecipeAdapter(Context mcontext, List<RecipeBean> arrlist2) {
        this.mcontext = mcontext;
        this.arrlist2 = arrlist2;
    }
    @Override
    public RecipeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View view= LayoutInflater.from(mcontext).inflate(R.layout.recycleritem,parent,false);
        ViewHolder myholder=new ViewHolder(view);
        return myholder;
    }
    @Override
    public void onBindViewHolder(final RecipeAdapter.ViewHolder myholder, int i) {
        final RecipeBean courseInfo= arrlist2.get(i);
        myholder.ke_name.setText(courseInfo.getCpName());
        myholder.ke_des.setText(courseInfo.getDes());
        Glide.with(mcontext).load(courseInfo.getSmallImg()).into(myholder.ke_img);
        myholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mcontext, RecipeDetailActivity.class);
                intent.putExtra("data" ,courseInfo);
                mcontext.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount() {
        return arrlist2.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ke_img;
        TextView ke_name,ke_des;
        public ViewHolder(View itemView) {
            super(itemView);
            ke_img=itemView.findViewById(R.id.ke_img);
            ke_name=itemView.findViewById(R.id.ke_name);
            ke_des=itemView.findViewById(R.id.ke_des);
        }
    }

}